package com.iot.ic.tcp.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

@Component
public class SendCustomNettyConfig {


    // reactor多线程模型中的acceptor
    private NioEventLoopGroup bossGroup = new NioEventLoopGroup(2, new NamedThreadFactory("Server-Boss"));

    // reactor多线程模型中的threadPool
    private NioEventLoopGroup workerGroup = new NioEventLoopGroup(8, new NamedThreadFactory("Server-Worker"));

    // bossGroup的线程数
    private int bossThreadCount;

    // workerGroup的线程数
    private int workerThreadCount;
    private InetSocketAddress socketAddress;
    @Value("${systemConfig.sendCustomPort}")
    public int portNumber;
    protected ChannelInitializer<? extends Channel> channelInitializer;

    public synchronized NioEventLoopGroup getBossGroup() {
        if (null == bossGroup) {
            if (0 >= bossThreadCount) {
                bossGroup = new NioEventLoopGroup();
            } else {
                bossGroup = new NioEventLoopGroup(bossThreadCount);
            }
        }
        return bossGroup;
    }


    public synchronized NioEventLoopGroup getWorkerGroup() {
        if (null == workerGroup) {
            if (0 >= workerThreadCount) {
                workerGroup = new NioEventLoopGroup();
            } else {
                workerGroup = new NioEventLoopGroup(workerThreadCount);
            }
        }
        return workerGroup;
    }

    public int getBossThreadCount() {
        return bossThreadCount;
    }

    public void setBossThreadCount(int bossThreadCount) {
        this.bossThreadCount = bossThreadCount;
    }

    public int getWorkerThreadCount() {
        return workerThreadCount;
    }

    public void setWorkerThreadCount(int workerThreadCount) {
        this.workerThreadCount = workerThreadCount;
    }

    public synchronized InetSocketAddress getSocketAddress() {
        if (null == socketAddress) {
            socketAddress = new InetSocketAddress(portNumber);
        }
        return socketAddress;
    }

    public void setSocketAddress(InetSocketAddress socketAddress) {
        this.socketAddress = socketAddress;
    }

    public int getPortNumber() {
        return portNumber;
    }

    public void setPortNumber(int portNumber) {
        this.portNumber = portNumber;
    }

}